package com.aricksm.xuan.activaty02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridActivity extends AppCompatActivity {

    List<MenuItem> dataList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_activity);

        GridView gridViewMenu = (GridView) findViewById(R.id.grid_view_menu);

        dataList = new ArrayList<>();

        dataList.add(new MenuItem("审前检查", R.drawable.menu1));
        dataList.add(new MenuItem("菜单2", R.drawable.menu2));
        dataList.add(new MenuItem("菜单3", R.drawable.menu3));
        dataList.add(new MenuItem("菜单4", R.drawable.menu4));
        dataList.add(new MenuItem("菜单5", R.drawable.menu5));
        dataList.add(new MenuItem("菜单6", R.drawable.menu6));
        dataList.add(new MenuItem("菜单7", R.drawable.menu7));
        dataList.add(new MenuItem("菜单8", R.drawable.menu3));
        dataList.add(new MenuItem("菜单9", R.drawable.menu4));
        dataList.add(new MenuItem("菜单10", R.drawable.menu5));
        dataList.add(new MenuItem("菜单11", R.drawable.menu6));
        dataList.add(new MenuItem("菜单12", R.drawable.menu7));

        MenuAdapter menuAdapter = new MenuAdapter();
        gridViewMenu.setAdapter(menuAdapter);
    }

    class MenuAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View menuView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, null);

            TextView tvMenuName = (TextView) menuView.findViewById(R.id.tv_menu);
            tvMenuName.setText(dataList.get(position).menuName);

            ImageView imageViewMenu = (ImageView) menuView.findViewById(R.id.image_menu);
            imageViewMenu.setImageResource(dataList.get(position).menuDrawableId);


            return menuView;
        }
    }

    class MenuItem{
        String menuName;
        int menuDrawableId;

        MenuItem(String menuName, int menuDrawableId){
            this.menuName = menuName;
            this.menuDrawableId = menuDrawableId;
        }
    }


}
